<template>
    <div>
        <div class="header-info">
            <p>{{action_name}}-用户</p>
            <div v-if="jv.main.user.isAdmin">
                <el-button size="mini" @click="$router.push('/admin-user/add')" v-if="action=='edit'">新建
                </el-button>
                <el-button size="mini" @click="reset_click" v-if="info.id"> 重置密码</el-button>
                <el-button size="mini" @click="save_click" type="primary"> 保存</el-button>
            </div>
        </div>
        <el-row :gutter="12" type="flex" ref="card">
            <el-col>
                <el-card shadow="always">
                    <div slot="header">
                        <span>基本信息</span>
                    </div>
                    <kv label="姓名">
                        <el-input v-model="info.name" chk="*"/>
                    </kv>
                    <kv label="登录名">
                        <el-input v-model="info.loginName" chk="*"/>
                    </kv>
                    <kv label="密码" v-if="action == 'add'">
                        <div>
                            <el-input v-model="info.password"
                                      chk=": if( value.length<8 ) return '长度不能小于8'; if( /^[\w\d]+$/.test(value) == false) return '只能包含数字和字母'; if( /\d/.test(value) == false) return '必须包含数字';if( /\w/.test(value) == false) return '必须包含字母'; "/>
                            <div>长度必须大于8位，必须包含数字和字母</div>
                        </div>
                    </kv>
                    <kv label="手机号">
                        <el-input v-model="info.mobile" chk="*"/>
                    </kv>
                    <kv label="管理员">
                        <selector type="radio" chk="*" v-model="info.isAdmin" :clearable="false"
                                  :data="{true:'管理员',false:'普通用户'}" :readOnly="!jv.main.user.isAdmin"/>
                    </kv>
                </el-card>
            </el-col>
        </el-row>

    </div>
</template>
<style scoped>
    >>> .kv .k {
        flex: 4 !important;
        min-width: auto !important;
    }
</style>
<script>
    /**
     * Created by CodeGenerator
     */
    export default {
        components: {},
        props: {
            id: {type: String, default: ""}
        },
        data() {
            return {
                info: {},

            }
        },
        computed: {
            action() {
                return this.id ? "edit" : "add";
            },
            action_name() {
                return {add: "添加", edit: "修改"}[this.action]
            }
        },
        mounted() {
            this.loadData();
        },
        methods: {
            loadData() {
                if (this.id) {
                    debugger;
                    this.$http.post("/admin-user/detail/" + this.id).then(res => {
                        this.info = res.data.data;
                    });
                }
            },
            reset_click() {
                jv.confirm("确定要重置密码吗?").then(res => {
                    this.$http.post("/admin-user/resetPassword/" + this.info.id).then(res => {
                        jv.info("设置成功");
                    })
                });
            },
            save_click() {
                //校验
                if (this.$refs.card.chk() == false) {
                    return;
                }

                this.$http.post("/admin-user/save", this.info).then(res => {
                    jv.info(this.action_name + " 成功");
                    if (this.action == "add") {
                        this.$router.push("/admin-user/edit/" + res.data.value)
                    } else if (this.action == "edit") {
                        this.$router.push("/admin-user/list")
                    }
                })
            }
        }
    }
</script>
